local extension = Package:new("tea_ex_a")
extension.extensionName = "wild_collection"
Fk:loadTranslationTable{
  ["tea_ex_a"] = "茶水闲设一",
  ["tea"] = "茶水",
  }
local U = require "packages/utility/utility"

local slash_include_cards = {
  --标准
  "duel", "collateral", "savage_assault", "crossbow", 
  "qinggang_sword", "spear", "blade", "double_swords", "axe", 
  "halberd", "kylin_bow", "ice_sword", "nioh_shield", 
  --军争
  "guding_blade", "fan", "vine", 
  --用间
  "yitian_sword", "women_dress", 
  --明忠
  "glittery_armor", "seven_stars_sword", "steel_lance",
  --星玉
  "sjade__talisman", "flood_attack", "chain_barrier", "attacktheunprepared", "tiger_spear",
  "taiping_flag", "qianghu_mask", "cuffed_armor", 
}

local dianman = General(extension, "tea__dianman", "wei", 4)
local jiyong = fk.CreateTriggerSkill{
  name = "tea_jiyong",
  anim_type = "offensive",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card then
      return table.contains(slash_include_cards, data.card.trueName) and not player:prohibitUse(Fk:cloneCard("slash"))
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("slash")
    local tos = table.filter(room:getOtherPlayers(player), function(p)
      return not player:isProhibited(p, card)
    end)
    tos = table.map(tos, Util.IdMapper)
    local to = room:askForChoosePlayers(player, tos, 1, 1, "#jiyong_choose", self.name) or {}
    if #to > 0 then
      to = to[1]
      self.cost_data = to
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("slash")
    local use = {
      from = player.id,
      tos = {{self.cost_data}},
      card = card,
      extra_data = {
        bypass_distances = true,
        bypass_times = true,
      }
    }
    room:useCard(use)
  end,
}
local chenglie_active = fk.CreateActiveSkill{
  name = "#tea_chenglie_active",
  prompt = "chenglie_active_prompt",
  target_num = 1,
  card_num = 1,
  can_use = Util.FalseFunc,
  expand_pile = function (self)
    return Self:getCardIds(Player.Judge)
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 
    and (Fk:currentRoom():getCardArea(to_select) == Player.Equip or table.contains(Self:getCardIds(Player.Judge), to_select))
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and #selected_cards == 1 and to_select ~= Self.id and
    Self:canMoveCardInBoardTo(Fk:currentRoom():getPlayerById(to_select), selected_cards[1])
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    if room:getCardArea(effect.cards[1]) == Player.Equip then
      U.moveCardIntoEquip(room, target, effect.cards[1], self.name, true, player)
    elseif room:getCardArea(effect.cards[1]) == Player.Judge then
      room:moveCardTo(effect.cards[1], Player.Judge, target, fk.ReasonJustMove, self.name, nil, true, player.id)
    end
    if not target.dead and target:isWounded() then
      room:recover{
        who = target,
        num = 1,
        recoverBy = player,
        skillName = self.name,
      }
    end
  end,
}
local chenglie = fk.CreateTriggerSkill{
  name = "tea_chenglie",
  anim_type = "control",
  events = {fk.Damage, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      local room = player.room
      local cards = player:getCardIds(Player.Equip)
      for _, id in ipairs(player:getCardIds(Player.Judge)) do
        table.insertIfNeed(cards, id)
      end
      if table.find(cards, function(id)
        return table.find(room:getOtherPlayers(player), function(p)
          return player:canMoveCardInBoardTo(p, id)
        end)
      end) then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:askForUseActiveSkill(player, "#tea_chenglie_active", nil, true)
  end,
}
chenglie:addRelatedSkill(chenglie_active)
dianman:addSkill(jiyong)
dianman:addSkill(chenglie)
Fk:loadTranslationTable{
  ["tea__dianman"] = "典满",
  ["#tea__dianman"] = "武夫无负",
  ["designer:tea__dianman"] = "cc",
  ["illustrator:tea__dianman"] = "Dra",
  ["cv:tea__dianman"] = "茶社",
  ["tea_jiyong"] = "继勇",
  [":tea_jiyong"] = "当你使用一张描述含“【杀】”的非【杀】牌结算后，你可以视为使用一张无距离次数限制的【杀】。",
  ["#jiyong_choose"] = "你可以发动“继勇”，使用一张无距离次数限制【杀】。",
  ["tea_chenglie"] = "承烈",
  [":tea_chenglie"] = "每回合限一次，当你造成或受到伤害后，你可以移动你场上的一张牌若如此做，你令获得牌的角色回复1点体力。",
  ["#tea_chenglie_active"] = "承烈",
  ["chenglie_active_prompt"] = "你可以发动承烈，将你场上的一张牌置入其他角色的对应区域内。",

  ["$tea_jiyong1"] = "双戟遗留三十载，如今亦能断敌酋。",
  ["$tea_jiyong2"] = "继父勇烈，斩将溃敌何惜手劳？",
  ["$tea_chenglie1"] = "满，承父志，思国恩，今请长缨。",
  ["$tea_chenglie2"] = "此番悍战，方不辱先父所授。",
  ["~tea__dianman"] = "先父立死营门，小儿辈岂可坐辱？",
}
local majun = General(extension, "tea__majun", "wei", 3)
local qiaozhu = fk.CreateActiveSkill{
  name = "tea_qiaozhu",
  anim_type = "support",
  target_num = 0,
  card_num = 1,
  prompt = "#tea_qiaozhu-invoke",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and player:hasSkill(self)
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:showCards(effect.cards)
    local ctype = Fk:getCardById(effect.cards[1]):getTypeString()
    local cards = room:askForCard(player, 1, 999, true, self.name, true, ".|.|.|.|.|^"..ctype, "#qiaozhu_recast:::"..ctype) or {}
    if #cards > 0 then
      room:recastCard(cards, player, self.name)
    end
  end,
}
local qiaozhu_trigger = fk.CreateTriggerSkill{
  name = "#tea_qiaozhu_trigger",
  refresh_events = {fk.GameStart, fk.EventAcquireSkill, fk.EventLoseSkill, fk.Deathed},
  can_refresh = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill("tea_qiaozhu", true)
    elseif event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == self and not table.find(player.room:getOtherPlayers(player), function(p) return p:hasSkill("tea_qiaozhu", true) end)
    else
      return target == player and player:hasSkill("tea_qiaozhu", true, true) and
        not table.find(player.room:getOtherPlayers(player), function(p) return p:hasSkill("tea_qiaozhu", true) end)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart or event == fk.EventAcquireSkill then
      if player:hasSkill("tea_qiaozhu", true) then
        for _, p in ipairs(room:getOtherPlayers(player)) do
          room:handleAddLoseSkills(p, "tea_qiaozhu&", nil, false, true)
        end
      end
    elseif event == fk.EventLoseSkill or event == fk.Deathed then
      for _, p in ipairs(room:getOtherPlayers(player)) do
        room:handleAddLoseSkills(p, "-tea_qiaozhu&", nil, false, true)
      end
    end
  end,
}
local qiaozhu_active = fk.CreateActiveSkill{
  name = "tea_qiaozhu&",
  anim_type = "support",
  target_num = 0,
  card_num = 1,
  prompt = "#tea_qiaozhu&-invoke",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and player:hasSkill(self)
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = table.filter(room:getOtherPlayers(player), function(p)
      return p:hasSkill("tea_qiaozhu") and not p.dead
    end)[1]
    room:obtainCard(target, effect.cards, true, fk.ReasonGive, player.id)
    if not target.dead then
      room:setPlayerMark(target, "tea_qiaozhu-block-phase", 1)
      player:showCards(effect.cards)
      local ctype = Fk:getCardById(effect.cards[1]):getTypeString()
      local cards = room:askForCard(target, 1, 999, true, self.name, true, ".|.|.|.|.|^"..ctype, "#qiaozhu_recast:::"..ctype) or {}
      if #cards > 0 then
        room:recastCard(cards, target, self.name)
        local givenCards = room:askForCard(target, 1, 999, true, self.name, true, ".|.|.|.|.|"..ctype, "#qiaozhu_give:"..player.id.."::"..ctype) or {}
        if #givenCards > 0 then
          room:obtainCard(player, givenCards, false, fk.ReasonGive)
        end
      end
      room:setPlayerMark(target, "tea_qiaozhu-block-phase", 0)
    end
  end,
}
local jingnu = fk.CreateTriggerSkill{
  name = "tea_jingnu",
  anim_type = "support",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player.room.current and not player.dead and player:hasSkill(self) and player:getMark("tea_qiaozhu-block-phase") == 0 then
      local room = player.room
      local current = room.current
      if not current.dead and current:getAttackRange() > 0 then
        for _, move in ipairs(data) do
          if (move.to == player.id and move.toArea == Card.PlayerHand) or move.from == player.id then
            local n = 0
            for _, info in ipairs(move.moveInfo) do
              if info.cardId then
                n = n + 1
              end
            end
            if n >= player:getAttackRange() then
              self.cost_data = n
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data then
      room:addPlayerMark(room.current, "@jingnu_buff-turn", self.cost_data)
    end
  end,
}
local jingnu_attackrange = fk.CreateAttackRangeSkill{
  name = "#tea_jingnu_attackrange",
  correct_func = function (self, from, to)
    return from:getMark("@jingnu_buff-turn")
  end,
}
local jingnu_targetmod = fk.CreateTargetModSkill{
  name = "#tea_jingnu_targetmod",
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and player:getMark("@jingnu_buff-turn") > 0 and scope == Player.HistoryPhase then
      return player:getMark("@jingnu_buff-turn")
    end
  end,
}
Fk:addSkill(qiaozhu_active)
qiaozhu:addRelatedSkill(qiaozhu_trigger)
jingnu:addRelatedSkill(jingnu_attackrange)
jingnu:addRelatedSkill(jingnu_targetmod)
majun:addSkill(qiaozhu)
majun:addSkill(jingnu)
Fk:loadTranslationTable{
  ["tea__majun"] = "马钧",
  ["#tea__majun"] = "没渊瑰璞",
  ["designer:tea__majun"] = "菖蒲",
  ["illustrator:tea__majun"] = "7.game",
  ["cv:tea__majun"] = "茶社",
  ["tea_qiaozhu"] = "巧筑",
  [":tea_qiaozhu"] = "一名角色出牌阶段限一次，其可以交给你一张牌并展示，你可以重铸任意张非此类型的牌，然后你可以交给其任意张此类型的牌。",
  ["tea_qiaozhu&"] = "巧筑",
  [":tea_qiaozhu&"] = "出牌阶段限一次，你可以交给拥有“巧筑”的角色一张牌并展示之，其可以重铸任意张此类型的牌。",
  ["#qiaozhu_recast"] = "巧筑：你可以重铸任意张 非%arg。",
  ["#qiaozhu_give"] = "巧筑：你可以交给 %src 任意张 %arg。",
  ["tea_qiaozhu&"] = "巧筑",
  ["#tea_qiaozhu-invoke"] = "你可以展示一张牌，然后可以重铸任意张非此类型牌。",
  ["#tea_qiaozhu&-invoke"] = "你可以交给马钧一张牌并展示之，然后其可以重铸任意张非此类型牌。",
  ["tea_jingnu"] = "精弩",
  [":tea_jingnu"] = "当你一次性获得或失去至少X张牌时（你于回合外发动“巧筑”重铸与给出牌除外），你可以令当前回合角色本回合攻击范围和使用"..
  "【杀】的次数上限+X（X为当前回合角色攻击范围）。",
  ["@jingnu_buff-turn"] = "精弩",

  ["$tea_qiaozhu1"] = "须臾之间，片木可任五十石之重。",
  ["$tea_qiaozhu2"] = "心怀巧思，可铸世间万物。",
  ["$tea_jingnu1"] = "精研器械之道，在细微而不在繁。",
  ["$tea_jingnu2"] = "工欲善其事，必先利其器。",
  ["~tea__majun"] = "吾技未展，憾留人间……",
}

local huojun = General(extension, "tea__huojun", "shu", 4, 4, General.Male)
local jianju = fk.CreateViewAsSkill{
  name = "tea_jianju",
  mute = true,
  pattern = "jink,nullification",
  card_filter = Util.FalseFunc,
  interaction = function()
    local names = {}
    for _, name in ipairs({"jink", "nullification"}) do
      local card = Fk:cloneCard(name)
      if (Fk.currentResponsePattern == nil and Self:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)) then
        table.insertIfNeed(names, card.name)
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self)
    if not self.interaction.data then return end
    if (self.interaction.data == "jink" and not table.find(Self:getCardIds("h"), function(id)
      return Fk:getCardById(id).color == Card.Red
    end)) or (self.interaction.data == "nullification" and not table.find(Self:getCardIds("h"), function(id)
      return Fk:getCardById(id).color == Card.Black
    end)) then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(table.filter(Self:getCardIds("h"), function(id)
      if self.interaction.data == "jink" then
        return Fk:getCardById(id).color == Card.Red
      elseif self.interaction.data == "nullification" then
        return Fk:getCardById(id).color == Card.Black
      end
    end))
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:getMark("tea_jianju_on_effect") > 0 and not player:isKongcheng()
  end,
  enabled_at_response = function(self, player, response)
    return player:getMark("tea_jianju_on_effect") > 0 and not player:isKongcheng()
  end,
}
local jianju_trigger = fk.CreateTriggerSkill{
  name = "#tea_jianju_trigger",
  anim_type = "defensive",
  mute = true,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player.dead or not player:hasSkill(self) then return end
    return target.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if target == player then 
      return room:askForSkillInvoke(player, self.name)
    elseif player:getMark("@@jianju_loseall-turn") > 0 and not room.current.dead and  room.current ~= player then
      local card = Fk:cloneCard("duel")
      if not player:prohibitUse(card) or not player:isProhibited(room.current, card) then
        return room:askForSkillInvoke(player, self.name, nil, "#jianju_ask_for_duel:"..target.id)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target == player then 
      player:broadcastSkillInvoke(self.name, table.random{1, 2})
      local num = #table.filter(room:getOtherPlayers(player), function(p)
        return player:inMyAttackRange(p)
      end)
      if num > 0 then
        player:drawCards(num, self.name)
      end
      if not player.dead then
        room:setPlayerMark(player, "tea_jianju_on_effect", 1)
      end
    else
      room:notifySkillInvoked(player, jianju.name, "defensive")
      player:broadcastSkillInvoke(jianju.name, table.random{5, 6})
      room:notifySkillInvoked(player, jianju.name, "offensive")
      local card = Fk:cloneCard("duel")
      card.skillName = jianju.name
      room:useCard{
        from = player.id,
        tos = {{room.current.id}},
        card = card,
      }
    end
  end,
  refresh_events = {fk.EventPhaseStart, fk.PreCardUse},
  can_refresh = function(self, event, target, player, data)
    if player.dead or not player:hasSkill(self) then return end
    local room = player.room
    if event == fk.EventPhaseStart and player.phase == Player.Start and target == player then
      return player:getMark("tea_jianju_on_effect") > 0
    elseif event == fk.PreCardUse and data.card and target == player then
      return target == player and table.contains(data.card.skillNames or {}, jianju.name) 
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      player.room:setPlayerMark(player, "tea_jianju_on_effect", 0)
    elseif event == fk.PreCardUse then
      player.room:notifySkillInvoked(player, jianju.name, "offensive")
      player:broadcastSkillInvoke(jianju.name, table.random{3, 4})
      if data.card:isVirtual() then
        if table.every(player:getCardIds("h"), function(id)
          return table.contains(data.card.subcards  or {}, id)
        end) then
          player.room:setPlayerMark(player, "@@jianju_loseall-turn", 1)
        end
      end
    end
  end,
}
jianju:addRelatedSkill(jianju_filter)
jianju:addRelatedSkill(jianju_trigger)
huojun:addSkill(jianju)
Fk:loadTranslationTable{
  ["tea__huojun"] = "霍峻",
  ["#tea__huojun"] = "蒹葭蕼荆",
  ["designer:tea__huojun"] = "cc",
  ["illustrator:tea__huojun"] = "jenny",
  ["cv:tea__huojun"] = "茶社",
  ["tea_jianju"] = "坚拒",
  [":tea_jianju"] = "结束阶段，你可以摸你攻击范围内含有的角色数张牌，若如此做，直到你的下个准备阶段，你可以将所有红色/黑色手牌当【闪】/【无懈可击】使用或打出。"..
  "你因使用“坚拒”转化的牌失去最后的手牌后，你可以于当前回合结束阶段视为对当前回合角色使用一张【决斗】。",
  ["#tea_jianju_trigger"] = "坚拒",
  ["@@jianju"] = "坚拒",
  ["@@jianju_loseall-turn"] = "坚拒-决斗",
  ["#jianju_ask_for_duel"] = "坚拒：你可以视为对%src使用一张【决斗】。",

  ["$tea_jianju1"] = "我势虽寡，然有余烈拒敌！",
  ["$tea_jianju2"] = "磐石在胸，意如峻岭之坚。",
  ["$tea_jianju3"] = "（使用闪/无懈时）协群力而俱守，聚众志以成城！",
  ["$tea_jianju4"] = "（使用闪/无懈时）量汝宵小技穷，皆为残兵游勇？",
  ["$tea_jianju5"] = "（使用决斗时）一鼓未破，再衰而竭，随我上马迎敌！",
  ["$tea_jianju6"] = "（使用决斗时）天时已至，地利即合，此我破阵良机！",
  ["~tea__huojun"] = "铁血丹心映日月，壮志凌云贯长虹……",
}
local yinguan = General(extension, "tea__yinguan", "shu", 3, 3, General.Male) 
local yingfa = fk.CreateActiveSkill{
  name = "tea_yingfa",
  anim_type = "offensive",
  mute = true,
  prompt = "#yingfa_prompt",
  target_num = 2,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return to_select ~= Self.id and #selected < 2
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local ask1 = effect.tos[1]
    local ask2 = effect.tos[2]
    local cards = effect.cards
    local targets = table.filter(room.alive_players, function(p)
      return p.id ~= ask1 and p.id ~= ask2
    end)
    room:notifySkillInvoked(player, self.name, "special")
    player:broadcastSkillInvoke(self.name, 1)
    targets = table.map(targets, Util.IdMapper)
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#yingfa_target_choose-ask", self.name, false, false)
    if not to then return end
    local seats = {ask1, ask2}
    room:sortPlayersByAction(seats)
    local answer = {}
    for i = 1, 2 do
      local ask = room:getPlayerById(seats[i])
      local use = room:askForUseCard(ask, "slash", "slash", "#yingfa_use-ask:"..to[1], true, {
        exclusive_targets = {to[1]},
        bypass_distances = true,
      })
      if use then
        table.insert(answer, use)
      end
    end
    if #answer == 2 then
      room:notifySkillInvoked(player, self.name, "special")
      player:broadcastSkillInvoke(self.name, 2)
      answer[1].tos = {{player.id}}
      answer[2].tos = {{player.id}}
    end
    if #answer > 0 then
      for _, using in ipairs(answer) do
        if not room:getPlayerById(using.tos[1][1]).dead then
          room:useCard(using)
        end
      end
    end
    if #answer == 0 then
      room:notifySkillInvoked(player, self.name, "special")
      player:broadcastSkillInvoke(self.name, 3)
      local list = {}
      for _, id in ipairs({ask1, ask2}) do
        if not room:getPlayerById(id):isNude() then
          table.insertIfNeed(list, id)
        end
      end
      if #list > 0 then
        local tos = room:askForChoosePlayers(player, list, 1, 1, "#yingfa_obtain_choose-ask", self.name, false)
        if tos then
          local card = room:askForCardChosen(player, room:getPlayerById(tos[1]), "he", self.name, "#yingfa_obtain-ask")
          if card then
            room:obtainCard(player, card, false, fk.ReasonPrey, player.id)
          end
        end
      end
    end
  end,
}
local zhiyue = fk.CreateTriggerSkill{
  name = "tea_zhiyue",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and not player.dead
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(3)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonPut,
      skillName = self.name,
      proposer = player.id
    })
    local choosecards = room:askForCard(player, 1, 3, false, self.name, true, ".|.|.|^(hand,equip)|.", "#zhiyue_choosecards", cards) or {}
    if #choosecards > 0 then
      local targets = room.alive_players
      targets = table.map(targets, Util.IdMapper)
      local toid = room:askForChoosePlayers(player, targets, 1, 1, "#zhiyue_chooseto", self.name, false)
      if toid then
        local to = room:getPlayerById(toid[1])
        room:obtainCard(to, choosecards, true, fk.ReasonGive, player.id)
        if not to.dead then
          local suits = room:askForChoices(to, {"log_heart", "log_diamond", "log_spade", "log_club"}, #choosecards, #choosecards, self.name, "#zhiyue-choice:::"..#choosecards, false)
          local discards = {}
          to:showCards(to.player_cards[Player.Hand])
          local inhand = to:getCardIds(Player.Hand)
          for _, id in ipairs(inhand) do
            if id > 0 then
              if table.contains(suits, Fk:getCardById(id):getSuitString(true)) then
                table.insert(discards, id)
              end
            end
          end
          if #discards > 0 then
            room:delay(500)
            room:throwCard(discards, self.name, to, to)
          end
        end
      end
    end
  end,
}
yinguan:addSkill(yingfa)
yinguan:addSkill(zhiyue)
Fk:loadTranslationTable{
  ["tea__yinguan"] = "殷观",
  ["#tea__yinguan"] = "楚之兰芳",
  ["designer:tea__yinguan"] = "菖蒲",
  ["illustrator:tea__yinguan"] = "柏桦",
  ["cv:tea__yinguan"] = "茶社",
  ["tea_yingfa"] = "应伐",
  [":tea_yingfa"] = "出牌阶段限一次，你可以令两名其他角色同时选择是否对你选择的另一名角色使用【杀】（无距离限制），若均选择：是这些【杀】目标改为你；否，你获得其中一名角色一张牌。",
  ["#yingfa_prompt"] = "发动“应伐”，选择两名出杀的其他角色。",
  ["#yingfa_target_choose-ask"] = "选择【杀】的目标。",
  ["#yingfa_use-ask"] = "应伐：你可以对 %src 使用一张【杀】，点击取消则视为选择“否”。",
  ["#yingfa_obtain_choose-ask"] = "应伐：你可以选择其中一名角色，获得其一张牌。",
  ["#yingfa_obtain-ask"] = "选择其一张牌获得之",
  ["tea_zhiyue"] = "制越",
  [":tea_zhiyue"] = "当你受到伤害后，你可以亮出牌堆顶的三张牌，并将其中任意张交给一名角色，该角色声明等量花色，展示所有手牌然后弃置此花色的所有手牌。",
  ["#zhiyue_choosecards"] = "制越：选择至多三张亮出牌。",
  ["#zhiyue_chooseto"] = "制越：将选择牌交给一名角色。",
  ["#zhiyue-choice"] = "选择%arg个花色，展示手牌并弃置这些花色的牌。",

  ["$tea_yingfa1"] = "今可效张倚之法，谬应伐蜀之策。",
  ["$tea_yingfa2"] = "（全选择是）地纷争，若共起刀兵，恐反自陷囹圄之中。",
  ["$tea_yingfa3"] = "（全选择否）吴以诡言诱之，主公可兴赞而不兴兵也。",
  ["$tea_zhiyue1"] = "如此进退之计，定可坐收吴、蜀之利。",
  ["$tea_zhiyue2"] = "吾等按兵不动，吴必不敢独取蜀地。",
  ["~tea__yinguan"] = "望主公思虑再三，切不可为吴先驱啊！",
}

local huanjie = General(extension, "tea__huanjie", "wu", 3, 3, General.Male)
local nianzhi = fk.CreateTriggerSkill{
  name = "tea_nianzhi",
  anim_type = "control",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) or player:isKongcheng() or not player.room.current then return false end
    for _, move in ipairs(data) do
      if move.from == player.id or (move.toArea == Player.Hand and move.to == player.id) then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Player.Hand and move.from == player.id) 
          or (move.toArea == Player.Hand and move.to == player.id) then
            local suits_record = {{"log_heart", 0}, {"log_diamond", 0}, {"log_spade", 0}, {"log_club", 0},}
            local sucess = 0
            for _, cid in ipairs(player:getCardIds(Player.Hand)) do
              sucess = 1
              if cid then
                local card = Fk:getCardById(cid)
                local suit = card:getSuitString(true)
                for _, v in ipairs(suits_record) do
                  if v[1] == suit then
                    v[2] = v[2] + 1
                  end
                end
              end 
            end
            local suc = 0
            local suit_num = 0
            local suit_type = {}
            if sucess == 1 then
              for _, n in ipairs(suits_record) do
                if n[2] > 0 then
                  suit_num = suit_num + 1
                  table.insert(suit_type, n[1])
                end
              end
              for _, i in ipairs(suits_record) do
                if i[2] > 0 and suc == 0 then
                  suc = i[2]
                elseif i[2] > 0 and i[2] ~= suc then
                  suc = 0
                  break
                end
              end
            end
            if suc > 0 then self.cost_data = {suit_num, suit_type} end
            return suc > 0
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local current = room.current
    local num = self.cost_data[1]
    local suits = self.cost_data[2]
    player:showCards(player.player_cards[Player.Hand])
    current:drawCards(num)
    if not current.dead then
      local mark = current:getMark("@nianzhi-turn")
      if mark == 0 then
        room:setPlayerMark(current, "@nianzhi-turn", suits)
      else
        for _, suit in ipairs(suits) do
          table.insertIfNeed(mark, suit)
        end
        room:setPlayerMark(current, "@nianzhi-turn", mark)
      end
    end
  end,
}
local nianzhi_prohibit = fk.CreateProhibitSkill{
  name = "#tea_nianzhi_prohibit",
  prohibit_use = function(self, player, card)
    if player:getMark("@nianzhi-turn") == 0 or not card then return end
    if table.contains(player:getMark("@nianzhi-turn"), card:getSuitString(true)) then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
}
local qisang = fk.CreateTriggerSkill{
  name = "tea_qisang",
  anim_type = "support",
  frequency = Skill.Limited,
  events = {fk.Death},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not target:isNude() and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForDiscard(player, 1, 1, true, self.name, true, nil, "#qisang-ask:"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards_id = target:getCardIds{Player.Hand, Player.Equip}
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#qisang-choose", self.name, false)
    room:obtainCard(to[1], cards_id, false, fk.ReasonPrey)
  end,
}
nianzhi:addRelatedSkill(nianzhi_prohibit)
huanjie:addSkill(nianzhi)
huanjie:addSkill(qisang)
Fk:loadTranslationTable{
  ["tea__huanjie"] = "桓阶",
  ["#tea__huanjie"] = "诣表乞丧",
  ["designer:tea__huanjie"] = "菖蒲",
  ["cv:tea__huanjie"] = "茶社",
  ["illustrator:tea__huanjie"] = "biou09",
  ["tea_nianzhi"] = "捻止",
  [":tea_nianzhi"] = "你的手牌数变化后，若其中每种花色的牌数均相等，你可以展示所有手牌并令当前回合角色摸等量于花色数张牌，然后其本回合不能使用这些花色的牌。",
  ["@nianzhi-turn"] = "捻止",
  ["tea_qisang"] = "乞丧",
  [":tea_qisang"] = "限定技，其他角色死亡时，你可以弃置一张牌并选择一名角色，其获得死亡角色的所有牌。",
  ["#qisang-ask"] = "乞丧：你可以弃置一张牌，然后选择一名角色，其获得 %src 的所有牌。",
  ["#qisang-choose"] = "乞丧：选择一名存活角色",

  ["$tea_nianzhi1"] = "福祸之间，宜审慎权衡，勿失其度也。",
  ["$tea_nianzhi2"] = "攻伐之事，非急在一时，而在全局之谋定而后动也。",
  ["$tea_qisang1"] = "诣表乞丧，得亡者以安息。",
  ["$tea_qisang2"] = "愿赴险地，以乞亡者之丧。",
  ["~tea__huanjie"] = "全福远祸，竟成空谈……",
}

local zhonglimu = General(extension, "tea__zhonglimu", "wu", 4, 4, General.Male)
local pingyue = fk.CreateViewAsSkill{
  name = "tea_pingyue",
  pattern = "slash",
  anim_type = "offensive",
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("slash")
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:getAttackRange() > 0
  end, 
  enabled_at_response = function(self, player, response)
    return player:getAttackRange() > 0 and not response
  end, 
  before_use = function(self, player, use)
    player.room:addPlayerMark(player, "@pingyue_attackrange-round", 1)
  end,
  after_use = function(self, player, use)
    if not player.dead and player:getAttackRange() > 0 then
      player.room:addPlayerMark(player, "pingyue_attackrange-round", 1)
    end
  end,
}
local pingyue_attackrange = fk.CreateAttackRangeSkill{
  name = "#tea_pingyue_attackrange",
  correct_func = function (self, from, to)
    return 0 - from:getMark("pingyue_attackrange-round")
  end,
}
local anjing = fk.CreateTriggerSkill{
  name = "tea_anjing",
  anim_type = "offensive",
  events = {fk.RoundStart, fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player.dead then return end
    if event == fk.RoundStart then
      return player:getMark("@anjing_attackrange") < 5
    else
      return not table.every(player.room.alive_players, function(p)
        return p:getMark("anjing_record-phase") == p:getAttackRange()
      end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.RoundStart then
      return true
    else
      local room = player.room
      local card = Fk:cloneCard("slash")
      if not player:prohibitUse(card) then
        local use = room:askForUseCard(player, "slash", nil, "#anjing-ask", true, {
          bypass_times = true,
        })
        if use then
          self.cost_data = use 
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.RoundStart then
      player.room:addPlayerMark(player, "@anjing_attackrange", 1)
    else
      player.room:useCard(self.cost_data)
    end
  end,
  refresh_events = {fk.EventPhaseStart},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and not player.dead
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      room:setPlayerMark(p, "anjing_record-phase", p:getAttackRange())
    end
  end,
}
local anjing_attackrange = fk.CreateAttackRangeSkill{
  name = "#tea_anjing_attackrange",
  correct_func = function (self, from, to)
    return from:getMark("@anjing_attackrange")
  end,
}
pingyue:addRelatedSkill(pingyue_attackrange)
anjing:addRelatedSkill(anjing_attackrange)
zhonglimu:addSkill(pingyue)
zhonglimu:addSkill(anjing)
Fk:loadTranslationTable{
  ["tea__zhonglimu"] = "钟离牧",
  ["#tea__zhonglimu"] = "讨越安民",
  ["designer:tea__zhonglimu"] = "仁语",
  ["illustrator:tea__zhonglimu"] = "青衿",
  ["cv:tea__zhonglimu"] = "茶社",
  ["tea_pingyue"] = "平越",
  [":tea_pingyue"] = "当你需要使用一张【杀】时，若你的攻击范围大于0，你可以视为使用一张【杀】，然后你本轮攻击范围-1。",
  ["@pingyue_attackrange-round"] = "平越 -",
  ["tea_anjing"] = "安境",
  [":tea_anjing"] = "每轮开始时，你攻击范围+1（至多+5）。每阶段结束时，若有角色攻击范围与此阶段开始时不相等，你可以使用一张【杀】。",
  ["@anjing_attackrange"] = "安境 +",
  ["#anjing-ask"] = "安境：你可以使用一张【杀】",

  ["$tea_pingyue1"] = "平越挥剑，斩寇如麻，誓守我边疆无虞！",
  ["$tea_pingyue2"] = "尔等贼寇，纵遁千里之外，亦难逃吾剑下诛杀之运！",
  ["$tea_anjing1"] = "外敌侵境，诱骗百姓，当攻伐之。",
  ["$tea_anjing2"] = "外境内侵，宜速扑取，此救火贵速之势也。",
  ["~tea__zhonglimu"] = "未尽守土之责，愧对百姓之望……",
}

local bocai = General(extension, "tea__bocai", "qun", 4)
local zhuojun = fk.CreateTriggerSkill{
  name  = "tea_zhuojun",
  anim_type = "offensive",
  mute = true,
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player and data.card and player:getMark("tea_zhuojun_passed-phase") == 0 then
      local room = player.room
      local room = player.room
      local phase_event = room.logic:getCurrentEvent():findParent(GameEvent.Phase, true)
      local ids = {}
      room.logic:getEventsByRule(GameEvent.UseCard, 998, function (e)
        local data = e.data[1]
        if data.from == player.id then
          local targets = U.getActualUseTargets(room, data)
          room:sortPlayersByAction(targets)
          for _, id in ipairs(ids) do
            table.insertIfNeed(targets, id)
          end
          ids = targets or {}
        end
        return false
      end, phase_event.id)
      if #ids >= 2 then
        if ids[1] ~= ids[2] then
          room:setPlayerMark(player, "tea_zhuojun_passed-phase", 1)
          self.cost_data = {ids[1], ids[2]}
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to1 = room:getPlayerById(self.cost_data[1])
    local to2 = room:getPlayerById(self.cost_data[2])
    return room:askForSkillInvoke(player, self.name, nil, "#tea_zhuojun-invoke:"..to1.id..":"..to2.id)
  end,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke(self.name, table.random{1, 2})
    local room = player.room
    local to1 = room:getPlayerById(self.cost_data[1])
    local to2 = room:getPlayerById(self.cost_data[2])
    local array1 = {to1.id}
    local array2 = {to1.id}
    local nextOne = to1:getNextAlive()
    local lastOne = to1:getLastAlive()
    while nextOne ~= to1 and #array1 < 100 do
      table.insert(array1, nextOne.id)
      if nextOne == to2 then break end
      nextOne = nextOne:getNextAlive() 
    end
    while lastOne ~= to1 and #array2 < 100 do
      table.insert(array2, lastOne.id)
      if lastOne == to2 then break end
      lastOne = lastOne:getLastAlive() 
    end
    local array
    if #array1 ~= #array2 then
      array = #array1 < #array2 and array1 or array2
    elseif #array1 == 2 then
      array = array1
    else
      local choose = room:askForChoice(player, {"zhuojun_array1:"..to1.id, "zhuojun_array2:"..to1.id}, self.name, "#tea_zhuojun-choose")
      array = choose == "zhuojun_array1:"..to1.id and array1 or array2
    end
    if array then
      if table.contains(array, player.id) then
        table.removeOne(array, player.id)
      end
      local card = Fk:cloneCard("savage_assault")
      card.skillName = self.name
      local use = {
        from = player.id,
        tos = table.map(array, function(id)
          return {id}
        end),
        card = card,
      }
      room:useCard(use)
    end
  end,
  refresh_events = {fk.DamageCaused, fk.CardRespondFinished},
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(self, true, true) then return end
    if event == fk.DamageCaused then
      if data.card and target == player then
        return table.contains(data.card.skillNames or {}, self.name)
      end
    else
      if data.card and data.responseToEvent then
        if data.responseToEvent.card.name == "savage_assault" and table.contains(data.responseToEvent.card.skillNames or {}, self.name) then
          return data.card.name == "fire__slash" and not target.dead and not player.dead
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then
      room:notifySkillInvoked(player, self.name, "offensive")
      player:broadcastSkillInvoke(self.name, 3)
      data.damageType = fk.ThunderDamage
      return
    else
      room:notifySkillInvoked(player, self.name, "defensive")
      player:broadcastSkillInvoke(self.name, 4)
      room:damage{
        from = target, 
        to = player,
        damage = 1, 
        skillName = self.name, 
        damageType = fk.FireDamage,
      }
    end
  end,
}
bocai:addSkill(zhuojun)
Fk:loadTranslationTable{
  ["tea__bocai"] = "波才",
  ["#tea__bocai"] = "掣恪每特",
  ["designer:tea__bocai"] = "鲲鲲",
  ["illustrator:tea__bocai"] = "奇兵",
  ["cv:tea__bocai"] = "茶社",
  ["tea_zhuojun"] = "捉骏",
  [":tea_zhuojun"] = "当你于出牌阶段指定第二名目标的牌结算结束后，你可以视为对本阶段你指定的前两名目标间（含其）最短路径上任意名连续角色使用【南蛮入侵】，"..
  "此【南蛮入侵】造成的伤害均视为雷电伤害，若有角色打出火【杀】响应此牌，其对你造成1点火焰伤害。",
  ["zhuojun_array1"] = "从%src处逆时针开始",
  ["zhuojun_array2"] = "从%src处顺时针开始",
  ["#tea_zhuojun-choose"] = "捉骏：你须选择一条路径",
  ["#tea_zhuojun-invoke"] = "你可以对 %src 与 %dest 发动“捉骏”",

  ["$tea_zhuojun1"] = "今日擒朱儁，明日捉皇甫，天子，哼，亦是囊中之物！",
  ["$tea_zhuojun2"] = "我虽草莽，亦可撼汉之高树。",
  ["$tea_zhuojun3"] = "（造成伤害）黄天嘉我，祚以此功，汉家将相不过尔尔。",
  ["$tea_zhuojun4"] = "（受到伤害）皇甫竖子！我竟败于你手！",
  ["~tea__bocai"] = "朱儁小儿，汝等侥幸得胜……",
}

local zhengtai = General(extension, "tea__zhengtai", "qun", 3)
local tableSwap = function(Table, Pos1, Pos2)
  Table[Pos1], Table[Pos2] = Table[Pos2], Table[Pos1]
  return Table
end
local setZePile = function(player, cids, skillName)
  player:addToPile("$tea_ze_pile", cids, true, skillName, player.id)
end
local huize = fk.CreateTriggerSkill{
  name = "tea_huize",
  mute = true,
  events = {fk.GamePrepared, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.GamePrepared then
      return true
    else
      return player == target and player.phase == Player.Start
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.GamePrepared then
      return true
    else
      return player.room:askForSkillInvoke(player, self.name)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GamePrepared then
      room:notifySkillInvoked(player, self.name, "special")
      player:broadcastSkillInvoke(self.name, 1)
      local cids = room:getNCards(5)
      setZePile(player, cids, self.name)
    else
      room:notifySkillInvoked(player, self.name, "special")
      player:broadcastSkillInvoke(self.name, table.random{2, 3})
      if #(player:getPile("$tea_ze_pile") or {}) < 5 then return end
      local cards_data = {{"$tea_ze_pile", player:getPile("$tea_ze_pile")}}
      local choose_data = {
        to = nil,
        min = 2,
        max = 2,
        skillName = self.name,
        prompt = "#tea_kangze-choose",
      }
      local ret = room:askForPoxi(player, "AskForCardsChosen", cards_data, choose_data, false)
      local new_ret = table.filter(ret, function(id) return id ~= -1 end)
      local hidden_num = #ret - #new_ret
      if hidden_num > 0 then
        table.insertTable(new_ret,
        table.random(player:getPile("$tea_ze_pile"), hidden_num))
      end
      if #new_ret == 2 then
        local zePile = player:getPile("$tea_ze_pile")
        if table.contains(zePile, new_ret[1]) and table.contains(zePile, new_ret[2]) then
          player:showCards(new_ret)
          local swapPile = tableSwap(zePile, table.indexOf(zePile, new_ret[1]), table.indexOf(zePile, new_ret[2]))
          room:sendLog{
            type = "#KangZeSwapCards",
            from = player.id,
          }
          player:addToPile("$tea_ze_pile", swapPile, true, self.name, player.id)
          local num = math.max(math.abs(table.indexOf(zePile, new_ret[1]) - table.indexOf(zePile, new_ret[2])) - 1, 0)
          if num > 0 then
            local targets = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, num, "#tea_kangze-drawcards:::"..num, self.name)
            if #targets > 0 then
              room:sortPlayersByAction(targets)
              for _, pid in ipairs(targets) do
                local p = room:getPlayerById(pid)
                if not p.dead then p:drawCards(1, self.name) end
              end
            end
          end
        end
      end
    end
  end,
}
local chouren = fk.CreateActiveSkill{
  name = "tea_chouren",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  expand_pile = function(self)
    return Self:getPile("$tea_ze_pile")
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected)
    return #selected == 0
  end,
  can_use = function(self, player)
    local cards = player:getPile("$tea_ze_pile")
    local numberup, numberdown
    if table.every(cards, function(id)
      if not numberup then 
        numberup = Fk:getCardById(id).number
        return true
      elseif (Fk:getCardById(id).number or 0) >= numberup then
        numberup = Fk:getCardById(id).number
        return true
      else
        return false
      end
    end) or
    table.every(cards, function(id)
      if not numberdown then 
        numberdown = Fk:getCardById(id).number
        return true
      elseif (Fk:getCardById(id).number or 0) <= numberdown then
        numberdown = Fk:getCardById(id).number
        return true
      else
        return false
      end
    end) then
      return true
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cards = player:getPile("$tea_ze_pile")
    room:notifySkillInvoked(player, self.name, "offensive")
    player:broadcastSkillInvoke(self.name, 1)
    player:showCards(cards)
    if not player.dead and not target.dead then
      room:damage{
        from = player,
        to = target,
        damage = 2,
        skillName = self.name,
      }
      local new_rets = {}
      local length = #cards
      for i = 1, length do
        local randomOne = table.random(cards, 1)
        table.insert(new_rets, randomOne)
        table.removeOne(cards, randomOne)
      end
      if #new_rets == 5 then
        setZePile(player, new_rets, self.name)
      end
    end
  end,  
}
local tableMerge = function(tableOne, tableTwo)
  local arr = {}
  if not tableOne then tableOne = {} end
  if not tableTwo then tableTwo = {} end
  for _, i in ipairs(tableOne) do
    table.insert(arr, i)
  end
  for _, i in ipairs(tableTwo) do
    table.insert(arr, i)
  end
  return arr
end
local chouren_trigger = fk.CreateTriggerSkill{
  name = "#tea_chouren_trigger",
  refresh_events = {fk.RoundEnd},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(chouren) and player:isWounded() then
      local room = player.room
      local targets = table.filter(
        room.alive_players, function(p)
          return table.every(room:getOtherPlayers(p), function(p2)
            return p.hp >= p2.hp
          end)
        end) or {}
      if #targets == 1 then
        self.cost_data = targets[1]
        return room:askForSkillInvoke(targets[1], chouren.name, nil, "#tea_chouren_trigger-invoke:"..player.id)
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local from = self.cost_data
    player:broadcastSkillInvoke(chouren.name, 2)
    room:notifySkillInvoked(player, chouren.name, "special")
    room:recover{
      who = player,
      num = 1,
      recoverBy = from,
      skillName = self.name
    }
    if not player.dead and not from.dead then
      local cards_data = {
        {"$tea_ze_pile", player:getPile("$tea_ze_pile")},
        {"$hand", from:getCardIds("h")},
        {"$equip", from:getCardIds("e")},
      }
      local choose_data = {
        to = nil,
        skillName = self.name,
        prompt = "#tea_chouren-choose",
      }
      local ret = room:askForPoxi(from, "tea_chouren_swap", cards_data, choose_data)
      if #ret == 2 then
        local fromCard = table.filter(ret, function(id)
          return table.contains(from:getCardIds("he"), id)
        end)
        table.removeOne(ret, fromCard[1])
        if fromCard and ret then
          local zePile = player:getPile("$tea_ze_pile")
          local index = table.indexOf(zePile, ret[1])
          table.insert(zePile, index, fromCard[1])
          table.removeOne(zePile, ret[1])
          room:moveCardTo(tableMerge(ret, zePile), Card.Processing)
          setZePile(player, zePile, self.name)
          room:obtainCard(from, ret, false, fk.ReasonJustMove)
        end
      end
    end
  end,
}
Fk:addPoxiMethod{
  name = "tea_chouren_swap",
  card_filter = function(to_select, selected, data)
    if #selected == 0  then
      return true
    elseif #selected == 1 then
      local check = table.contains(data[1][2], selected[1]) and tableMerge(data[2][2], data[3][2]) or data[1][2]
      return table.contains(check, to_select)
    end
    return false
  end,
  feasible = function(selected)
    return #selected == 2
  end,
  prompt = function (data, extra_data)
    return extra_data.prompt
  end
}
chouren:addRelatedSkill(chouren_trigger)
zhengtai:addSkill(huize)
zhengtai:addSkill(chouren)
Fk:loadTranslationTable{
  ["tea__zhengtai"] = "郑泰",
  ["#tea__zhengtai"] = "掷飒名泽",
  ["designer:tea__zhengtai"] = "yyuaN",
  ["illustrator:tea__zhengtai"] = "柏桦",
  ["cv:tea__zhengtai"] = "茶社",
  ["tea_huize"] = "恢泽",
  [":tea_huize"] = "游戏开始时，你将牌堆顶五张牌背面朝上排列于武将牌上，称为“泽”。准备阶段，你可以展示并置换两张“泽”的位置，然后令至多X名角色各摸一张牌（X为这两张“泽之间的牌数）。",
  ["$tea_ze_pile"] = "泽",
  ["#tea_kangze-choose"] = "选择并交换两种“泽”",
  ["#KangZeSwapCards"] = "%from 交换了其两张“泽”",
  ["#tea_kangze-drawcards"] = "慷泽：你可以令至多 %arg 名角色各摸一张牌",
  ["tea_chouren"] = "筹刃",
  [":tea_chouren"] = "出牌阶段，若所有“泽”点数依次递增或递减，你可以展示之并对一名角色造成2点伤害，然后打乱之。每轮结束时，体力值唯一最多的角色可以令你回复1点体力，然后用一张牌替换一张“泽’。",
  ["$hand"] = "手牌",
  ["$equip"] = "装备",
  ["#tea_chouren-choose"] = "你可以将一张牌与“泽”交换",
  ["#tea_chouren_trigger-invoke"] = "筹刃：你可以将一张牌与 %src 的“泽”进行交换",
  ["tea_chouren_swap"] = "筹刃",

  ["$tea_huize1"] = "（游戏开始时）文公不才，但有百亩良田，望能结交天下之志士！",
  ["$tea_huize2"] = "家有良田数百顷，广结英豪布粮泽。",
  ["$tea_huize3"] = "慷慨待士，疏百亩阔吾义气。",
  ["$tea_chouren1"] = "（造成伤害时）筹谋已久，利刃出鞘，今朝便是诛董之时！",
  ["$tea_chouren2"] = "（换牌时）恢泽筹刃，谋略独步。",
  ["~tea__zhengtai"] = "国贼未除，吾等之后，何人可起颓运……",
}

local simafu = General(extension, "tea__simafu", "jin", 3)
local xunde = fk.CreateTriggerSkill{
  name = "tea_xunde",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) or player:getMark("@@xunde_prohibit-turn") > 0 then return false end
    for _, move in ipairs(data) do
      if move.from == player.id or (move.toArea == Player.Hand and move.to == player.id) then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Player.Hand and move.from == player.id) 
          or (move.toArea == Player.Hand and move.to == player.id) then
            return true
          end
        end
      end
    end
    return
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p)
      return #player:getCardIds(Player.Hand) == #p:getCardIds(Player.Hand)
    end)
    local choose = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#tea_xunde-choose", self.name, true) or {}
    if #choose > 0 then
      self.cost_data = choose
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data[1])
    if to == player then
      room:setPlayerMark(player, "@@xunde_prohibit-turn", 1)
    end
    to:drawCards(1, self.name)
  end
}
local lizang = fk.CreateTriggerSkill{
  name = "tea_lizang",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Finish and player:getMark("tea_lizang_canuse-turn") > 0 and not target.dead and not target:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local suits = {}
    while not player.dead and not target.dead do
      if target:isNude() or #suits >= 4 or #(table.filter(target:getCardIds("h"), function(id) return not target:prohibitDiscard(Fk:getCardById(id)) end) or {}) == 0 then break end
      local cid1 = room:askForDiscard(target, 1, 1, true, self.name, false, ".", "#tra_lizang-discard", true)
      if #cid1 > 0 then
        table.insertIfNeed(suits, Fk:getCardById(cid1[1]):getSuitString())
        if table.contains(target:getCardIds(Player.Hand), cid1[1]) and #target:getCardIds(Player.Hand) == 1 then
          room:throwCard(cid1[1], self.name, target, target)
          break
        else
          room:throwCard(cid1[1], self.name, target, target)
        end
      end
      if player:isNude() or #suits >= 4 or #(table.filter(player:getCardIds("h"), function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end) or {}) == 0 then break end
      local cid2 = room:askForDiscard(player, 1, 1, true, self.name, false, ".", "#tra_lizang-discard", true)
      if #cid2 > 0 then
        table.insertIfNeed(suits, Fk:getCardById(cid2[1]):getSuitString())
        if table.contains(player:getCardIds(Player.Hand), cid2[1]) and #player:getCardIds(Player.Hand) == 1 then
          room:throwCard(cid2[1], self.name, player, player)
          break
        else
          room:throwCard(cid2[1], self.name, player, player)
        end
      end
    end 
  end,
  refresh_events = {fk.EnterDying},
  can_refresh = function(self, event, target, player, data)
    return target and player:hasSkill(self, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "tea_lizang_canuse-turn", 1)
  end,
}
simafu:addSkill(xunde)
simafu:addSkill(lizang)
Fk:loadTranslationTable{
  ["tea__simafu"] = "司马孚",
  ["#tea__simafu"] = "忠心释道",
  ["designer:tea__simafu"] = "菖蒲",
  ["illustrator:tea__simafu"] = "鬼画府",
  ["cv:tea__simafu"] = "茶社",
  ["tea_xunde"] = "勋德",
  [":tea_xunde"] = "当你的手牌数发生变化后，你可以令一名手牌数等于你的角色摸一张牌，若该角色为你，则此技能本回台失效。",
  ["@@xunde_prohibit-turn"] = "勋德失效",
  ["#tea_xunde-choose"] = "勋德：你可以令一名手牌数等于你的角色摸一张牌，若为你，此技能本回合失效。",
  ["tea_lizang"] = "礼葬",
  [":tea_lizang"] = "每个回合的结束阶段，若本回合内有角色进入过濒死状态，你可与当前回合角色依次弃牌，直至以此法弃置的牌达到四种花色或一方失去最后一张手牌。",
  ["#tra_lizang-discard"] = "礼葬：弃置一张牌",

  ["$tea_xunde1"] = "谦逊避之，弗敢专美于前。",
  ["$tea_xunde2"] = "君者至尊，可以不仁，臣不可不忠。",
  ["$tea_lizang1"] = "股枕遗尸，臣子誓守魏室。",
  ["$tea_lizang2"] = "礼葬虽奢，难尽哀悼之情。",
  ["~tea__simafu"] = "礼葬遗尸，守魏之志不渝；今日身陨，犹怀故国之思……",
}

local wangjun = General(extension, "tea__wangjun", "jin", 4)
local lianfang = fk.CreateTriggerSkill{
  name = "tea_lianfang",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = #player:getCardIds(player.Equip) + 1
    local targets = table.filter(room.alive_players, function(p)
      return not p:isNude()
    end)
    targets = table.map(targets, Util.IdMapper)
    local tos = room:askForChoosePlayers(player, targets, 1, num, "#tea_lianfang-choose:::"..num, self.name, true) or {}
    if #tos > 0 then
      self.cost_data = tos
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = self.cost_data
    local targets = table.map(tos, function(id)
      return room:getPlayerById(id)
    end)
    local extraData = {
      num = 1,
      min_num = 1,
      include_equip = true,
      pattern = ".",
      reason = self.name,
    }
    local prompt = "#askForLianFang"
    local data = { "choose_cards_skill", prompt, false, extraData }
    local req = Request:new(targets, "AskForUseActiveSkill")
    req.focus_text = "#tea_lianfang_considering"
    for _, to in ipairs(targets) do
      req:setData(to, data)
    end

    local cards = {}
    for _, p in ipairs(targets) do
      local cids 
      if p.reply_ready then
        local replyCard = req:getResult(p).card
        cids = table.random(json.decode(replyCard).subcards, 1)
      else
        cids = table.random(p:getCardIds(Player.Hand), 1)
      end
      table.insertIfNeed(cards, cids[1])
      room:recastCard(cids, p)
    end
    if #cards > 0 then
      local types = {}
      if table.find(cards, function(id)
        return Fk:getCardById(id).type == Card.TypeBasic
      end) then
        table.insert(types, "basic")
      end
      if table.find(cards, function(id)
        return Fk:getCardById(id).type == Card.TypeTrick
      end) then
        table.insert(types, "trick")
      end
      if table.find(cards, function(id)
        return Fk:getCardById(id).type == Card.TypeEquip
      end) then
        table.insert(types, "equip")
      end
      local result = U.askforViewCardsAndChoice(player, cards, types, self.name, "#tea_lianfang-gain")
      if result then
        local dic = {basic = Card.TypeBasic, trick = Card.TypeTrick, equip = Card.TypeEquip}
        local type_c = dic[result]
        room:obtainCard(player, table.filter(cards, function(id)
          return Fk:getCardById(id).type == type_c
        end), true, fk.ReasonPrey)
      end
    end
  end,
}
local ranjiang = fk.CreateViewAsSkill{
  name = "tea_ranjiang",
  anim_type = "offensive",
  prompt = "#tea_ranjiang-prompt",
  card_filter = function(self, to_select, selected)
    if #selected == 1 then return false end
    return table.contains(Self:getCardIds(Player.Equip), to_select)
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then
      return nil
    end
    local c = Fk:cloneCard("fire__slash")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function(self, player)
    return player:getMark("@@tea_ranjiang_prohibit-turn") == 0
  end,
  enabled_at_response = function(self, player)
    return player:getMark("@@tea_ranjiang_prohibit-turn") == 0
  end,
}
local ranjiang_targetmod = fk.CreateTargetModSkill{
  name = "#rea_ranjiang_targetmod",
  bypass_distances = function(self, player, skill, card, to)
    return player:hasSkill(ranjiang) and (card or {}).skillName == "tea_ranjiang"
  end,
  bypass_times = function(self, player, skill, scope, card, to)
    return player:hasSkill(ranjiang) and (card or {}).skillName == "tea_ranjiang"
  end, 
  extra_target_func = function(self, player, skill, card)
    if player:hasSkill(ranjiang) and skill.trueName == "slash_skill" and card.name == "fire__slash" then
      return math.max(#player:getCardIds(Player.Equip) - 1, 0)
    end
  end,
}
local ranjiang_delay = fk.CreateTriggerSkill{
  name = "#tea_ranjiang_delay",
  refresh_events = {fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    if not (data.card or {}).skillNames then return end
    return player:hasSkill(ranjiang) and player == target and table.contains((data.card or {}).skillNames, "tea_ranjiang") and not data.damageDealt
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@tea_ranjiang_prohibit-turn", 1)
  end,
}
ranjiang:addRelatedSkill(ranjiang_targetmod)
ranjiang:addRelatedSkill(ranjiang_delay)
wangjun:addSkill(lianfang)
wangjun:addSkill(ranjiang)
Fk:loadTranslationTable{
  ["tea__wangjun"] = "王濬",
  ["#tea__wangjun"] = "长天落霞",
  ["designer:tea__wangjun"] = "有懒癌的wkc",
  ["illustrator:tea__wangjun"] = "错落宇宙",
  ["cv:tea__wangjun"] = "茶社",
  ["tea_lianfang"] = "连舫",
  [":tea_lianfang"] = "准备阶段，你可以令至多X+1名角色同时选择一张牌重铸，然后你获得其中一种类别的牌（X为你装备区牌数）。",
  ["#tea_lianfang-choose"] = "连舫：你可以选择至多 %arg 名角色，然后这些角色同时选择一张牌并依次重铸这些牌",
  ["#askForLianFang"] = "连舫：你可以选择重铸一张牌",
  ["askForLianFang"] = "连舫：选择重铸一张牌",
  ["#tea_lianfang-gain"] = "连舫：获得一个类别的所有牌",
  ["#tea_lianfang_considering"] = "连舫",
  ["tea_ranjiang"] = "燃江",
  [":tea_ranjiang"] = "你可以将装备区一张牌当火【杀】使用(无距离与次数限制)，结算结束后，若未造成伤害，此技能本回合失效。你使用的火【杀】至多指定X名角色为目标（X为你装备区牌数）。",
  ["#tea_ranjiang-prompt"] = "燃江，你可以将一张牌当火【杀】使用",
  ['@@tea_ranjiang_prohibit-turn'] = "燃江失效",

  ["$tea_lianfang1"] = "连舟覆江行为路，铁索横断水自开",
  ["$tea_lianfang2"] = "艨艟扬帆今何向？欲请长缨，一了天下事。",
  ["$tea_ranjiang1"] = "火势如虹，势不可挡，吴军败退即今朝。",
  ["$tea_ranjiang2"] = "铁索沉江、熔火映日，吴侯，可愿出城赏景？",
  ["~tea__wangjun"] = "金瓯缺，月未圆……",
}
return extension